/**
 * @(#) TotalIncomProxy.as
 *
 * @date		2009. 04. 23.
 * @version		1.0
 * @author		Jo,Chang Hun
 * @description	 연말정산  - Proxy
 * 				연말정산 총소득액 관리 
 */
package com.skt.common_hr.yearadjustment.model
{
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.yearadjustment.YearAdjustmentFacade;
	import com.skt.common_hr.yearadjustment.model.vo.AdditionSubtractionCntVO;
	import com.skt.common_hr.yearadjustment.model.vo.BasicSubtractionCntVO;
	import com.skt.common_hr.yearadjustment.model.vo.ComboFamilyVO;
	import com.skt.common_hr.yearadjustment.model.vo.DependentFamilyVO;
	import com.wcl.utils.DateUtil;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	

	public class BasicSubtractionProxy extends CommonProxy
	{
		public static const NAME:String = "BasicSubtractionProxy";
		public var msgStr:String;
		
		
		public var humanSubTractionCnt:BasicSubtractionCntVO;
		public var additionCnt:AdditionSubtractionCntVO;
		
		public var dependentFamily:ArrayCollection;
		public var relationComboData:ArrayCollection;
		public var comboFamily:ArrayCollection;
		public var comboFamilyType:ArrayCollection;
		
		//2013
		public var comboHndType:ArrayCollection;


		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function BasicSubtractionProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		
		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 
		 * @param pernr 사번
		 * @return void
		 */
		public function load(pernr:String, year:String):void
		{
			var param:Object = new Object();
			param["I_YEAR"] = year;
			param["I_PERNR"] = pernr;
			loadData("Z_HR_0080_NEW", param, result, fault); 
		}
		
		//20120113
		public var sYear:String;
		public function familyLoad(parnr:String, year:String):void{
			sYear = year;
			
			var param:Object = new Object();
			param["I_YEAR"] = year;
			param["I_PERNR"] = parnr;
			param["I_PGBN"] = "1";		//1조회 2저장
			//Alert.show("familyLoad: " + parnr + ", " + year);
			loadData("Z_HR_0084_NEW", param, familyResult, familyFault); 
		}
		
		public function save(parnr:String, year:String, dependentArr:Array):void{
			/* for(var i:int = 0;  i < dependentArr.length; i++){
				trace("row : " + i);
				trace("---------------------------");
				trace(dependentArr[i].KDSVH);
				trace(dependentArr[i].LNMHG);
				trace(dependentArr[i].FNMHG);
				trace(dependentArr[i].HNDID);
				trace(dependentArr[i].REGNO);
				trace("---------------------------");
			} */
			var param:Object = new Object();
			param["I_YEAR"] = year;
			param["I_PERNR"] = parnr;
			param["I_PGBN"] = "2";		//1조회 2저장
			var table:Object = new Object(); 
			//PsHrAlert.showMessage("가족 : " + dependentArr.length);
			table["ITAB"] = dependentArr;
			
			saveData("Z_HR_0084_NEW", param, table, saveResult, saveFault); 
		}
		
		//관계 콤보박스
		public function comboload(year:String):void
		{
			var param:Object = new Object();
			param["I_YEAR"] = year;
			loadData("Z_HR_0089_NEW", param, comboResult, comboFault); 
		}

		public function loadTypeComobo(year:String):void
		{
			var param:Object = new Object();
			param["I_YEAR"] = year;
			
			loadData("Z_HR_0300", param, typeComboResult, typeComboFault); 
		}
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function result(event:ResultEvent):void
		{
			if(retCode==0) {   
				trace( "suss to load totalIncom proxy");
				humanSubTractionCnt = parseSubTractionCnt(event.result.ITAB2[0]);
				additionCnt = parseAdditionCnt(event.result.ITAB3[0]);
				facade.sendNotification(YearAdjustmentFacade.LOAD_BASIC_SUBTRACTIONCNT_COMPLETE, this);
			}
			else {
				trace( "failed to load totalIncom: (" +event.result["RETCODE"] + ") "+event.result["RETTEXT"]);
				msgStr = event.result["RETTEXT"];
				facade.sendNotification(YearAdjustmentFacade.LOAD_BASIC_SUBTRACTIONCNT_FAILED, this);
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function fault(event:FaultEvent):void
		{
			trace("Fault message in totalIncom: " + event.fault.faultString);
			msgStr = event.fault.faultString;
			facade.sendNotification(YearAdjustmentFacade.LOAD_BASIC_SUBTRACTIONCNT_FAILED, this);
		}
		
		//부양가족조회 성공
		public function familyResult(event:ResultEvent):void
		{
			//Alert.show("familyResult: " + retCode);
			if(retCode==0) {  
				trace( "suss to load familyResult");
				dependentFamily = parseDependentFamily(event.result.ITAB);
				facade.sendNotification(YearAdjustmentFacade.LOAD_DEPENDENT_FAMILY_COMPLETE, this);
			}
			else {
				trace( "failed to load totalIncom: (" +event.result["RETCODE"] + ") "+event.result["RETTEXT"]);
				msgStr = event.result["RETTEXT"];
				facade.sendNotification(YearAdjustmentFacade.LOAD_DEPENDENT_FAMILY_FAILED, this);
			}
		}
		
		//부양가족조회 실패
		private function familyFault(event:FaultEvent):void
		{
			trace("Fault message in totalIncom: " + event.fault.faultString);
			msgStr = event.fault.faultString;
			facade.sendNotification(YearAdjustmentFacade.LOAD_DEPENDENT_FAMILY_FAILED, this);
		}
		
		//부양가족저장 성공
		public function saveResult(event:ResultEvent):void
		{
			if(retCode==0) {  
				trace( "suss to save familyResult");
				facade.sendNotification(YearAdjustmentFacade.SAVE_DEPENDENT_FAMILY_COMPLETE, this);
			}
			else {
				trace( "failed to load totalIncom: (" +event.result["RETCODE"] + ") "+event.result["RETTEXT"]);
				msgStr = event.result["RETTEXT"];
				facade.sendNotification(YearAdjustmentFacade.SAVE_DEPENDENT_FAMILY_FAILED, this);
			}
		}
		
		//부양가족저장 실패
		private function saveFault(event:FaultEvent):void
		{
			trace("Fault message in save: " + event.fault.faultString);
			msgStr = event.fault.faultString;
			facade.sendNotification(YearAdjustmentFacade.SAVE_DEPENDENT_FAMILY_FAILED, this);
		}
		
		//관계 콤보박스 조회 성공
		public function comboResult(event:ResultEvent):void
		{
			if(retCode==0) {  
				trace( "suss to save LOAD_COMBO_FAMILY_COMPLETE");
				comboFamily = parseComboFamily(event.result.ITAB);
				//2013 장애인 콤보박스 
				comboHndType = parseComboHndType(event.result.T_CODE);
				facade.sendNotification(YearAdjustmentFacade.LOAD_COMBO_FAMILY_COMPLETE, this);
			}
			else {
				trace( "failed to load LOAD_COMBO_FAMILY_FAILED: (" +event.result["RETCODE"] + ") "+event.result["RETTEXT"]);
				msgStr = event.result["RETTEXT"];
				facade.sendNotification(YearAdjustmentFacade.LOAD_COMBO_FAMILY_FAILED, this);
			}
		}
		
		//관계 콤보박스 조회 실패
		private function comboFault(event:FaultEvent):void
		{
			trace("Fault message in save: " + event.fault.faultString);
			msgStr = event.fault.faultString;
			facade.sendNotification(YearAdjustmentFacade.LOAD_COMBO_FAMILY_FAILED, this);
		}
		
		
		//관계 콤보박스 조회 성공
		public function typeComboResult(event:ResultEvent):void
		{
			if(retCode==0) {  
				trace( "suss to save LOAD_COMBO_FAMILY_COMPLETE");
				comboFamilyType = parseComboFamilyType(event.result.ITAB);
				facade.sendNotification(YearAdjustmentFacade.LOAD_COMBO_FAMILY_TYPE_COMPLETE, this);
			}
			else {
				trace( "failed to load LOAD_COMBO_FAMILY_FAILED: (" +event.result["RETCODE"] + ") "+event.result["RETTEXT"]);
				msgStr = event.result["RETTEXT"];
				facade.sendNotification(YearAdjustmentFacade.LOAD_COMBO_FAMILY_TYPE_FAILED, this);
			}
		}
		
		//관계 콤보박스 조회 실패
		private function typeComboFault(event:FaultEvent):void
		{
			trace("Fault message in save: " + event.fault.faultString);
			msgStr = event.fault.faultString;
			facade.sendNotification(YearAdjustmentFacade.LOAD_COMBO_FAMILY_TYPE_FAILED, this);
		}
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 * 
		 * @param
		 * @return 연말정산  MainList Value Object
		 */
		/**
		 * 개인정보
		 * 
		 * @param
		 * @return 개인정보 Value Object
		 */
		private function parseSubTractionCnt(retObj:Object):BasicSubtractionCntVO
		{
			var vo:BasicSubtractionCntVO = new BasicSubtractionCntVO();
			
			vo.SPOU		= retObj["SPOU"];		//배우자 공제
			vo.DEPSUM 	= retObj["DEPSUM"];		//부양가족 공제 합
			vo.DEPGR 	= retObj["DEPGR"];		//직계존속
			vo.DEPCH 	= retObj["DEPCH"];		//직계비속(자녀)
			vo.DEPCX 	= retObj["DEPCX"];		//직계비속(자녀외) 2008
			vo.DEPBS 	= retObj["DEPBS"];		//형제자매
			vo.DEPEX 	= retObj["DEPEX"];		//기타 2008
			vo.DEPSG 	= retObj["DEPSG"];		// 수급자
			vo.DEPAD 	= retObj["DEPAD"];		/// 위탁아동
			return vo;
		}
		
		private function parseAdditionCnt(retObj:Object):AdditionSubtractionCntVO
		{
			var vo:AdditionSubtractionCntVO = new AdditionSubtractionCntVO();
			
			vo.OLD01	= retObj["OLD01"];
			vo.OLD02	= retObj["OLD02"];
			vo.HNDID	= retObj["HNDID"];
			vo.HUSID	= retObj["HUSID"];
			vo.CHDID	= retObj["CHDID"];
			vo.MUCHD	= retObj["MUCHD"];
			vo.ADCID	= retObj["ADCID"];
			//vo.SIGPR	= retObj["SIGPR"]; // rfc Z_HR_0082_NEW 에서 처리함 
			
			return vo;
		}
		
		private function parseDependentFamily(arr:ArrayCollection):ArrayCollection
		{
			var dCheckDate:Date = new Date();
			dCheckDate.setFullYear(parseInt(sYear), 11, 31);
			dCheckDate.setHours(0,0,0,0);
			
			var returnArr:ArrayCollection = new ArrayCollection();
			trace("frist arr : " + arr.length);
			//Alert.show("parseDependentFamily: " + arr.length);
			for each(var reg:Object in arr){
				var vo:DependentFamilyVO = new DependentFamilyVO();
				
				vo.FNAGBN	= reg["FNAGBN"];	//내외국인구분
				vo.SUBTY	= reg["SUBTY"];		//하위 유형
				vo.STEXT	= reg["STEXT"];		//하위 유형 이름
				vo.OBJPS	= reg["OBJPS"];		//오브젝트식별
				vo.KDSVH	= reg["KDSVH"];		//자녀와의 관계
				vo.ATEXT	= reg["ATEXT"];		//텍스트(20자)
				vo.DPTYP	= reg["DPTYP"];		//부양가족 유형
				vo.DPTXT	= reg["DPTXT"];		//부양가족유형텍스트
				vo.LNMHG	= reg["LNMHG"];		//성 (한글)
				vo.FNMHG	= reg["FNMHG"];		//이름 (한글)
				vo.REGNO	= reg["REGNO"];		//주민등록번호 (한국)
				vo.DPTID	= reg["DPTID"];		//부양가족 구성원에 대한 지시자
				vo.HNDID	= reg["HNDID"];		//장애인에 대한 지시자
				vo.WOMEE	= reg["WOMEE"];		//부녀자세대주/맞벌이 여부
				vo.CHDID	= reg["CHDID"];		//자녀보호비용을 위한 면제지시자
				vo.ADCID	= reg["ADCID"];		//입양자녀여부
				vo.ADPDT	= DateUtil.stringToDateTime(reg["ADPDT"]);		//입양일자
				
				//20120113				
				vo.BEGDA	= DateUtil.stringToDateTime(reg["BEGDA"]);		//
				
				//2013
				vo.HNDCD	= reg["HNDCD"];		//장애구분코드
				vo.HNDID	= reg["HNDID"];		//장애구분여부				
				vo.SIGPR	= reg["SIGPR"];		//한부모 공제 여부 
				
				if(dCheckDate > vo.BEGDA) {
					vo.bCanDEL = false;	
				} else {
					vo.bCanDEL = true;
				}
				
				
				returnArr.addItem(vo);
			}
			
			return returnArr;
		}
		
		private function parseComboFamily(arr:ArrayCollection):ArrayCollection
		{
			var returnArr:ArrayCollection = new ArrayCollection();
			for each(var reg:Object in arr){
				var vo:ComboFamilyVO = new ComboFamilyVO();
				
				vo.CODE		= reg["KDSVH"];
				vo.TEXT		= reg["ATEXT"];
				vo.SUBTY	= reg["SUBTY"];
				vo.DPTYP	= reg["DPTYP"];
				vo.DPTXT	= reg["DPTXT"];
				vo.NOINPUT	= reg["NOINPUT"]=="X" ? true:false;
				returnArr.addItem(vo);
			}
			returnArr.addItemAt({CODE:'',TEXT:'관계선택'}, 0);
			return returnArr;
		}
		private function parseComboFamilyType(arr:ArrayCollection):ArrayCollection
		{
			var returnArr:ArrayCollection = new ArrayCollection();
			for each(var reg:Object in arr){
				var vo:Object = new Object();
				
				vo.CODE		= reg["DPTYP"];
				vo.TEXT		= reg["DPTYX"];
				returnArr.addItem(vo);
			}
			returnArr.addItemAt({CODE:'',TEXT:'유형선택'}, 0);
			return returnArr;
		}
		
		private function parseComboHndType(arr:ArrayCollection):ArrayCollection
		{
			var returnArr:ArrayCollection = new ArrayCollection();
			for each(var reg:Object in arr){
				var vo:Object = new Object();
				
				vo.CODE		= reg["CODE"];
				vo.TEXT		= reg["TEXT"];
				returnArr.addItem(vo);
			}
			returnArr.addItemAt({CODE:'',TEXT:'구분선택'}, 0);
			return returnArr;
		}
		
	}
}